ActiveAdmin.register Timetable do
  config.batch_actions = false

  filter :company_name, as: :string
  filter :store_name, as: :string
  filter :store_city, as: :string
  filter :coach_name, as: :string
  filter :coach_kind, as: :string, label: '教练类型'
  filter :course_name, as: :string
  filter :course_en_name, as: :string
  filter :price
  filter :member_price
  filter :class_start_time

  index do
    column :id
    column :company_name
    column :store_name
    column :store_city
    column :course_name
    column :coach_name
    column :coach_kind
    column :class_start_time
    column :class_end_time
    column :market_price
    column :price
    column :member_price
    column :place
    column :members
    column :non_members
    column :lining
    column :quota
    column :status_i18n
    column :signed_at
  end

  controller do
    def scoped_collection
      Timetable.left_joins(:company, :store, :classroom, :course, :admission, :coach, :instead_coach).select(
        :id,
        :store_id,
        'companies.name as company_name',
        'stores.name as store_name',
        'stores.city AS store_city',
        :classroom_id,
        'classrooms.name as classroom_name',
        :coach_id,
        :instead_coach_id,
        'instead_coaches_timetables.name as instead_coach_name',
        :course_id,
        "CONCAT(courses.en_name,' | ',courses.name) AS course_name",
        'courses.usage AS course_usage',
        'courses.category AS course_category',
        :bookable,
        "coalesce(array_length(timetables.appointed_member,1),0) as members",
        "coalesce(array_length(timetables.experience_member,1),0)as non_members",
        "CONCAT(coaches.name,'(',coaches.en_name,')') AS coach_name",
        'coaches.employee_id AS coach_employee_id',
        'coaches.kind AS coach_kind',
        :bookable,
        :class_start_time,
        :class_end_time,
        'AGE(timetables.class_end_time,timetables.class_start_time) as during',
        :market_price,
        :price,
        :member_price,
        :place,
        :appointed_member,
        "admissions.attendance as attendance",
        "admissions.inspector as inspector",
        "admissions.deduction as deduction",
        "cardinality(queues) AS lining",
        :status,
        :pricing,
        :income,
        :signed_at
      )
    end

    def index
      if params[:format].eql?("csv") || params[:format].eql?("xls")
        sql = Timetable.ransack(params[:q]).result.to_sql
        filter = sql.split("WHERE")[1]
        csv = CSV.generate(headers: true) do |row|
          row << %w(编号 集团 门店编号 门店 城市 教室编号 教室 课程编号 课程 课程等级 收费类型 课程类型 教练 教练工号 教练类型 被代课编号 被代课教练 是否需要预约 上课时间 下课时间 时长 市场价 非会员价 会员价 剩余名额 会员预约 非会员预约 登记人数 巡查 其他扣款 排队 名额 容纳人数 渠道预约 状态 版权费 课时费 补贴
服务费 实际收入 签到时间 批次号 修改时间)
          Timetable.left_joins(:company, :store, :classroom, :course, :admission, :coach, :instead_coach).where(filter).pluck(
            Arel.sql(
              "timetables.id, companies.name as company_name, timetables.store_id, stores.name as store_name,
                stores.city AS store_city,timetables.classroom_id, classrooms.name as classroom_name,timetables.course_id,
                CONCAT(courses.en_name,' | ',courses.name) AS course_name, courses.level as course_level,
                CASE courses.usage WHEN 0 THEN '免费课' WHEN 1 THEN '收费课' WHEN 2 THEN 'GX-收费课' WHEN 3 THEN 'PT-收费课' ELSE '线上课' END AS course_usage, courses.category AS course_category,
                CONCAT(coaches.name,'(',coaches.en_name,')') AS coach_name, coaches.employee_id AS coach_employee_id, CONCAT(coaches.kind, '/', coaches.level) AS coach_kind,
                timetables.instead_coach_id, instead_coaches_timetables.name as instead_coach_name,
                bookable, class_start_time, class_end_time, AGE(timetables.class_end_time,timetables.class_start_time)/60 as during,
                market_price, price, member_price, place, coalesce(array_length(timetables.appointed_member,1),0) as members, coalesce(array_length(timetables.experience_member,1),0) as non_members,
                admissions.attendance as attendance, admissions.inspector as inspector, admissions.deduction->'amount' as deduction, cardinality(queues) AS lining,
                (coalesce(array_length(timetables.appointed_member,1),0) + coalesce(array_length(timetables.experience_member,1),0) + timetables.place) AS quota, timetables.capacity, 0 AS c_members,
                CASE timetables.status WHEN 4 THEN '已发布' WHEN 5 THEN '已签到' WHEN 6 THEN '迟到' WHEN 7 THEN '缺席' WHEN 8 THEN '停课' ELSE '未发布' END AS t_status,
                pricing->'copyright' AS copyright, pricing->'grade' AS grade, pricing->'perquisite' AS perquisite, pricing->'service' AS service, 0 AS income, signed_at,
                batch_no,timetables.updated_at"
            )
          ).each do |column|
            row << column
          end
        end
        send_data(("\uFEFF" + +csv), type: Mime[:csv], disposition: "attachment; filename=课表信息-#{Time.current.to_i}.csv")
      else
        index!
      end
    end
  end
end
